<?php

namespace Biz\service\Store;


use Biz\model\store\StoreCustomCategory;
use Biz\model\store\StoreCustomCategoryWiki;

class CustomCategoryService
{
    /**
     * 编辑分类
     * @param int $storeId
     * @param $data
     * @return bool
     */
    public static function editCategory(int $storeId, $data): bool
    {
        $dataCategoryIds = array_column($data, 'category_id');
        $dataCategoryIds = array_filter($dataCategoryIds);

        $list = StoreCustomCategory::selectByWhere([
            'category_state' => StoreCustomCategory::STATE_NORMAL,
            'store_id' => $storeId,
        ]);
        $categoryIds = array_column($list, 'id');
        $delCategoryIds = array_diff($categoryIds, $dataCategoryIds);
        foreach ($delCategoryIds as $id) {
            self::delCategory($id, $storeId);
        }
        foreach ($data as $row) {
            if ($row['category_id']) {
                StoreCustomCategory::updateData(['sort' => $row['sort'], 'title' => $row['title']],
                    ['id' => $row['category_id'], 'store_id' => $storeId]);
            } else {
                StoreCustomCategory::insertData([
                    'sort' => $row['sort'],
                    'title' => $row['title'],
                    'store_id' => $storeId,
                ]);
            }
        }

        return true;
    }

    /**
     * 删除分类
     * @param int $categoryId
     * @param int $storeId
     * @return bool
     */
    public static function delCategory(int $categoryId, int $storeId): bool
    {
        StoreCustomCategory::updateData(['category_state' => StoreCustomCategory::STATE_DEL],
            ['id' => $categoryId, 'store_id' => $storeId]);
        StoreCustomCategoryWiki::updateData(['custom_state' => StoreCustomCategoryWiki::STATE_CANCEL],
            ['category_id' => $categoryId, 'store_id' => $storeId]);

        return true;
    }

    /**
     * 分类列表
     * @param int $storeId
     * @return array
     */
    public static function bGetCategoryList(int $storeId): array
    {
        $result[] = [
            'category_id' => "0",
            'title' => '全部',
            'sort' => "0",
        ];
        $list = StoreCustomCategory::selectByWhere([
            'store_id' => $storeId,
            'category_state' => StoreCustomCategory::STATE_NORMAL,
            "ORDER" => ['sort' => 'ASC', 'id' => 'DESC'],
        ]);
        if ($list) {
            foreach ($list as $row) {
                $result[] = [
                    'category_id' => $row['id'],
                    'title' => $row['title'],
                    'sort' => $row['sort'],
                ];
            }
        }

        return $result;
    }

    public static function bGetCategoryListForWeb(int $storeId): array
    {
        $result[] = [
            'category_id' => "0",
            'title' => '全部',
            'sort' => "0",
        ];
        $categoryIdMap = StoreCustomCategoryWiki::selectByWhere([
                "store_id" => $storeId,
                "custom_state" => StoreCustomCategoryWiki::STATE_NORMAL,
            ], "category_id") ?? [];
        $list = StoreCustomCategory::selectByWhere([
            'store_id' => $storeId,
            'category_state' => StoreCustomCategory::STATE_NORMAL,
            'id' => $categoryIdMap,
            "ORDER" => ['sort' => 'ASC', 'id' => 'DESC'],
        ]);
        if ($list) {
            foreach ($list as $row) {
                $result[] = [
                    'category_id' => $row['id'],
                    'title' => $row['title'],
                    'sort' => $row['sort'],
                ];
            }
        }

        return $result;
    }


}
